// miniprogram/pages/chDetail/chDetail.js
const db = wx.cloud.database();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    skstatus:[],//授课数组
    textareaValue:[],//备注数组
    
  },
  add(e){
    let index = e.currentTarget.dataset.index;
    if ((this.data.textareaValue[index] == null || this.data.textareaValue[index] == this.data.chList[index].remark || this.data.textareaValue[index].trim() == '') && (this.data.skstatus[index] == null || this.data.skstatus[index] == this.data.chList[index].status)){
      wx.showToast({
        title: '您没有做任何操作!'
      });
      return false;
    }
    //进度表
    if (this.data.chList[index].id != null) {
          getApp().sendRequest({
            url: "progessUpdateOrAdd",
            method: "GET",
            data: {
              id: this.data.chList[index].id,
              classId: this.data.classId,
              bookId: this.data.bookId,
              chId: this.data.chList[index].ch.id,
              remark: this.data.textareaValue[index]==undefined?'':this.data.textareaValue[index],
              status: this.data.skstatus[index] != null ? 1 : this.data.chList[index].status
            },
            success: res => {
              wx.showToast({
                title: '修改进度成功'
              });
            },
            fail: resf => {
              wx.showToast({
                title: '修改进度失败'
              });
            }
          })
    }else{
      getApp().sendRequest({
        url: "progessUpdateOrAdd",
        method: "GET",
        data: {
          classId: this.data.classId,
          bookId: this.data.bookId,
          chId: this.data.chList[index].ch.id,
          remark: this.data.textareaValue[index] == undefined ? '' : this.data.textareaValue[index],
          status: this.data.skstatus[index] == true ? 1 : 0
        },
        success: res => {
          wx.showToast({
            title: '新增进度成功!',
          })
        },
        fail: res => {
          wx.showToast({
            title: '新增进度失败!',
          })
        }
      })
    }
  },
  //授课按钮
  sk(e) {
    let index = e.currentTarget.dataset.index;
    this.data.skstatus[index] = e.detail.value;
  },
  //文本框
  textareaChange(e) {
    let index = e.currentTarget.dataset.index;
    let value = e.detail.value.trim();
    this.data.textareaValue[index] = value;
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      bookId: options.bookId,
      classId: options.classId
    })

    //章节表
    getApp().sendRequest({
      url: "selectChListByBookId",
      method: "GET",
      data: {
        bid:this.data.bookId
      },
      success: res => {
        this.setData({
          chList: res.data.data
        })
        console.log(this.data.chList)
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})